package com.cssw.fyzb.busi.util;



import cn.hutool.core.util.StrUtil;
import com.cssw.fyzb.core.model.dodb.BankDistrict;
import com.cssw.fyzb.core.model.vo.user.BankDistrictTreeVo;
import com.cssw.fyzb.core.model.vo.user.DistrictTreeNodeVo;
import com.cssw.fyzb.core.model.dodb.District;

import java.util.ArrayList;
import java.util.List;

public class ListToTreeUtil {

    public static List<DistrictTreeNodeVo> getTree(List<District> funcs) {
        if (null == funcs || funcs.isEmpty()) {
            return null;
        }
        List<DistrictTreeNodeVo> DistrictTreeNodeVos = new ArrayList<>();
        DistrictTreeNodeVo DistrictTreeNodeVo = null;
        for (District func : funcs) {
            DistrictTreeNodeVo = new DistrictTreeNodeVo();
            DistrictTreeNodeVo.setDistrictCode(func.getDistrictCode());
            DistrictTreeNodeVo.setDistrictName(func.getDistrictName());
            DistrictTreeNodeVo.setDistrictType(func.getDistrictType());
            DistrictTreeNodeVo.setSupDistrictCode(func.getSupDistrictCode());
            DistrictTreeNodeVos.add(DistrictTreeNodeVo);
        }
//        DistrictTreeNodeVos.sort(Comparator.comparing(DistrictTreeNodeVo::getSort, Comparator.nullsLast(Integer::compareTo)));
        List<DistrictTreeNodeVo> treeList = build(DistrictTreeNodeVos, "0");

        return treeList;
    }

    /**
     * 两层循环实现建树
     *
     * @param treeNodes 传入的树节点列表
     * @return
     */
    private static List<DistrictTreeNodeVo> build(List<DistrictTreeNodeVo> treeNodes, String root) {

        List<DistrictTreeNodeVo> trees = new ArrayList<>();

        for (DistrictTreeNodeVo treeNode : treeNodes) {
            if (root.equals(treeNode.getSupDistrictCode().toString())) {
                trees.add(findChildren(treeNode, treeNodes));
            }
        }
        return trees;
    }


    /**
     * 递归查找子节点
     *
     * @param treeNodes
     * @return
     */
    private static DistrictTreeNodeVo findChildren(DistrictTreeNodeVo treeNode, List<DistrictTreeNodeVo> treeNodes) {
        for (DistrictTreeNodeVo it : treeNodes) {
            if (StrUtil.equals(treeNode.getDistrictCode().toString(), it.getSupDistrictCode().toString())) {
                if (treeNode.getChildren() == null) {
                    treeNode.setChildren(new ArrayList<>());
                }
                treeNode.add(findChildren(it, treeNodes));
            }
        }
        return treeNode;
    }

    public static List<BankDistrictTreeVo> getBankDistrictTree(List<BankDistrict> bankDistricts) {
        if (null == bankDistricts || bankDistricts.isEmpty()) {
            return null;
        }
        List<BankDistrictTreeVo> bankDistrictTreeVos = new ArrayList<>();
        BankDistrictTreeVo bankDistrictTreeVo = null;
        for (BankDistrict district : bankDistricts) {
            bankDistrictTreeVo = new BankDistrictTreeVo();
            bankDistrictTreeVo.setValue(district.getValue());
            bankDistrictTreeVo.setTitle(district.getTitle());
            bankDistrictTreeVo.setSupValue(district.getSupValue());
            bankDistrictTreeVos.add(bankDistrictTreeVo);
        }
        List<BankDistrictTreeVo> treeList = buildBankDistrict(bankDistrictTreeVos, "0");

        return treeList;
    }

    /**
     * 两层循环实现建树
     *
     * @param treeNodes 传入的树节点列表
     * @return
     */
    private static List<BankDistrictTreeVo> buildBankDistrict(List<BankDistrictTreeVo> treeNodes, String root) {

        List<BankDistrictTreeVo> trees = new ArrayList<>();

        for (BankDistrictTreeVo treeNode : treeNodes) {
            if (root.equals(treeNode.getSupValue().toString())) {
                trees.add(findBankDirstrictChildren(treeNode, treeNodes));
            }
        }
        return trees;
    }


    /**
     * 递归查找子节点
     *
     * @param treeNodes
     * @return
     */
    private static BankDistrictTreeVo findBankDirstrictChildren(BankDistrictTreeVo treeNode, List<BankDistrictTreeVo> treeNodes) {
        for (BankDistrictTreeVo it : treeNodes) {
            if (StrUtil.equals(treeNode.getValue().toString(), it.getSupValue().toString())) {
                if (treeNode.getCities() == null) {
                    treeNode.setCities(new ArrayList<>());
                }
                treeNode.add(findBankDirstrictChildren(it, treeNodes));
            }
        }
        return treeNode;
    }
}

